﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    /// <summary>
    /// 首冲礼包
    /// </summary>
    public class UIActivityFirstRecharge : UIActivityTemplate
    {
        public GameObject rewardRoot;
        public GameObject rewardNamePref;
        public GameObject btnCharge;
        public GameObject imgGetted;
        public UISprite wordOnBtn;      //充值、领取，两种情况
        public GameObject iconPref;
        
        int curStatues = 0;
        int rewardIndex = 0;

        public override void OnCoverPageRemove()
        {
            base.OnCoverPageRemove();
            if (activity.panel.activedType == activityType)
            {
                CSActivityInfo msg = new CSActivityInfo();
                msg.activityId = this.activityId;
                NetAPI.SendHttp(OpDefine.CSActivityInfo, msg, MsgCallback);
            }
        }

        public override void Show()
        {
            base.Show();
            CSActivityInfo msg = new CSActivityInfo();
            msg.activityId = this.activityId;
            NetAPI.SendHttp(OpDefine.CSActivityInfo, msg, MsgCallback);
        }

        void MsgCallback(NetWork.Packets.Packet packet, bool success)
        {
            if(success)
            {
                SCActivityInfo msg = packet.kBody as SCActivityInfo;

                if (msg.itemList.Count <= 0)
                    return;

                activityId = msg.activityId;
                curStatues = msg.itemList[0].status;
                rewardIndex = msg.itemList[0].index;
                UpdateRewards(msg.itemList);
            }
        }

        void UpdateRewards(List<ActivityItem> itemList)
        {
            float x = -120;
            for (int i = 0; i < itemList[0].rewardList.Count; i++)
            {
                float scale = 1.2f;
                int fontSize = 19;
                if (i != 1)
                {
                    scale = 0.9f;
                    fontSize = 21;
                }

                if (iconPref != null)
                {
                    string id = itemList[0].rewardList[i].id;
                    GameObject obj = Instantiate(iconPref);
                    obj.SetActive(true);
                    obj.GetComponent<CommonIcon>().SetData(id, true, itemList[0].rewardList[i].num);
                    obj.transform.parent = rewardRoot.transform;
                    obj.transform.localScale = new Vector3(scale, scale, scale);
                    obj.transform.localPosition = new Vector3(x, 10, 0);

                    GameObject label = Instantiate(rewardNamePref);
                    label.SetActive(true);
                    label.GetComponent<UILabel>().text = TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(id));
                    label.GetComponent<UILabel>().fontSize = fontSize;
                    label.transform.parent = obj.transform;
                    label.transform.localScale = Vector3.one;
                    label.transform.localPosition = new Vector3(0, -65, 0);
                }

                x += 120;
            }
            
            if (wordOnBtn != null)
            {
                if (curStatues == 0)
                    wordOnBtn.spriteName = "UI_activity_shouchong_chognzhi";
                else if (curStatues == 1)
                    wordOnBtn.spriteName = "UI_activity_shouchong_lignqu";

                btnCharge.SetActive(curStatues != 2);
                imgGetted.SetActive(curStatues == 2);
            }
        }

        protected override void PrepareData()
        {
            this.activityType = ActivityType.FirstRecharge;
        }

        public void OnRechargeBtn()
        {
            if(curStatues == 0)
            {
                PageManager.Instance.OpenPage("ChargePage", "");
            }
            else
            {
                CSActivityReward msg = new CSActivityReward();
                msg.activityId = this.activityId;
                msg.index = rewardIndex;

                NetAPI.SendHttp(OpDefine.CSActivityReward, msg, GetRewardCallback);
            }
        }

        void GetRewardCallback(NetWork.Packets.Packet packet, bool success)
        {
            if(success)
            {
                SCActivityReward msg = packet.kBody as SCActivityReward;
                StaticData.playerData.AddResultObj(msg.get);

                curStatues = msg.itemList[0].status;
                UpdateRewards(msg.itemList);

                CommonMethod.ShowGetObjs(msg.get);

                activity.Open("");

                StaticData.playerData.showFirstRecharge = false;
                StaticData.redTips.SetPointState(RedPointTip.Red.firstRechargeGift, 0);
                StaticData.redTips.ReduceActivitys();
            }
        }
    }
}